# encoding: utf-8
# module func
# from (pre-generated)
# by generator 1.145

import math


def quadratic(a, b, c):
    if not (isinstance(a, (int, float)) \
            and isinstance(b, (int, float)) \
            and isinstance(c, (int, float))):
        raise TypeError("bad param type")
    dr = b * b - 4 * a * c
    if dr > 0:
        return (-b + math.sqrt(dr)) / (2 * a), (-b - math.sqrt(dr)) / (2 * a)
    elif dr == 0:
        return b / (2 * a)
    else:
        print("此方程没有实数根")


(x1, x2) = quadratic(2, 10, 3)

print("x1=%s", x1)
print("x2=%s", x2)


# 参数默认值
def power(x, n=1):
    s = 1
    while n > 0:
        n -= 1
        s *= x
    return s


print("result:", power(5, 2))
print("result:", power(5))


def print2(id, name, age=10, city="厦门"):
    print(id, name, age, city)


print2("123", "测试")
# 指定特定参数的值
print2("123", "测试", city="福州")


# 默认值一定要是不变对象,如果是可变对象,则下次调用时就会改变
def add(L=[]):
    L.append("END")
    return L


print(add())
print(add([1]))
print(add())


def add2(L=None):
    if L == None:
        L = []
    L.append("end")
    return L


print(add2())
print(add2([1]))
print(add2())


# 可变参数
def sum(*nums):
    s = 0
    for num in nums:
        s += num
    return s


print(sum())
print(sum(1, 2, 3, 4, 5))
# *list或者tuple,传递可变参数
print(sum(*[1, 2, 3, 4, 5]))


# 关键字参数
def person(name, age, **extra):
    print("name:", name, ",age:", age, ",others:", extra)


person("sdl", 30)
person("sdl", 30, city="厦门", height=170)
extraInfo = {"city": "厦门", "height": 170}
person("sdl", 30, **extraInfo)


def person2(name, age, **info):
    print("name:", name, ",age:", age)
    if "job" in info:
        print("job:", info.get("job"))
    if "city" in info:
        print("city:", info.get("city"))


person2("abc", 10)
person2("abc", 10, job="IT", city="福州")


# 参数组合
def f1(a, b, c=0, *args, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)


def f2(a, b, c=0, *, d, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)


f1(1, 2, 3, 'a', 'b')
f1(1, 2, 3, 'a', 'b', x=99)
f2(1, 2, d=99, ext=None)

args = (1, 2, 3, 4)
kw = {'d': 99, 'x': '#'}
f1(*args, **kw)
args = (1, 2, 3)
f2(*args, **kw)


# 递归函数
def fact(n, sum=1):
    if n <= 1:
        return sum
    return fact(n - 1, n * sum)


print("result:", fact(3))
